package com.pubpage.flow.flow_search;

import java.util.ArrayList;
import java.util.Hashtable;

import com.pub.database.*;
import com.pub.web.WebFunction;

public class Business {
   QueryBean gqbnDAO=null;

   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }

   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view, String pstrUserNO) throws Exception{
       if(view.getData_kind().equals("find_flow")){
           return getDataViewHtml(view.getDataView(),view.getDataViewForm(),view);
       }

       return "";
    }

    String getDataViewHtml(DataView dataView,DataViewForm DataViewForm,View view) throws Exception{
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(getDataViewPageAction(view));

        //設定要query的table list(以逗號隔開)
        DataViewForm.setTablesList(view.getFlow_main_table() + ",pnl_employee,floweng_setup");

        //依欄位查詢值組合sql where
        DataViewForm.setSQLCondition(getDataViewWhere(view));

        //設定排序的欄位
        DataViewForm.setOrderByFields("flow_num");

        //排序的方法(正序或倒序)
        DataViewForm.setOrderBySeq("asc");

        //group by 的欄位(以逗號隔開)
        DataViewForm.setGroupBy(null);

        //Having 的欄位(有設定group by時此欄位才有作用)
        DataViewForm.setHaving(null);

        //將欄位相關設定值設入DataViewForm
        DataViewForm.setColRelationUseArrayList(getDataViewColRelation(dataView,view));

        //設定DataView Header的<tr> html屬性
        DataViewForm.setTableHeaderHtml("class='td_head_grid' align='center'  width='680px'");

        DataViewForm.setJumpPageButtonShow(false);   //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(true);         //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(false);       //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false);           //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(true);         //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(true);         //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false);          //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false);            //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false);          //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(true);  //當ResultSet無資料是否要秀Header
        DataViewForm.setAddNewShow(false);           //是否秀出新增鈕

        return dataView.getOutputDataView(DataViewForm);
   }

    /**
     * getDataViewColRelation
     *
     * @param dataView DataView
     * @param view View
     * @return ArrayList
     */
    private ArrayList<Hashtable<String,Object>> getDataViewColRelation(DataView dataView, View view) {
        ArrayList<Hashtable<String,Object>> laltColRelation = new ArrayList<Hashtable<String,Object>> ();
        Hashtable<String,Object> lhtColRelationDetail = new Hashtable<String,Object>();
        ArrayList<String> laltLinkParamList=new ArrayList<String>();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "STATUS");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "HIDDEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML, "<INPUT type='hidden' name='status%sn%' class='input_noborder' readonly value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "STATUS");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");


        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();


        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, view.getFlow_code() + "_FLOW_NUM");
        lhtColRelationDetail.put(dataView.STR_FIELD_SUB_SQL, "FLOW_NUM");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'流程單號'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "LINK");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,"<a target='_blank' href=");
        lhtColRelationDetail.put(dataView.STR_LINK_URI,"'"+ view.getQuery_page() +"'");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, view.getFlow_code() + "_FLOW_NUM");
        laltLinkParamList.add("flow_num=");
        laltLinkParamList.add(view.getFlow_code() + "_FLOW_NUM");
        laltLinkParamList.add("status=");
        laltLinkParamList.add("STATUS");
        lhtColRelationDetail.put(dataView.STR_LINK_PARAM,laltLinkParamList);

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "''");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "HIDDEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML, "<INPUT type='hidden' name='flow_num%sn%' class='input_noborder' readonly value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, view.getFlow_code() + "_FLOW_NUM");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "APP_DATE");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'申請日期'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "EMPNO_NAME");
        lhtColRelationDetail.put(dataView.STR_FIELD_SUB_SQL, "employee_name_c");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'當事人'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLOW_DESC");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'流程站別'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "'跟催' as wkf_urge");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'跟催'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML, "<INPUT type='button' name='btnWkfUrge' class='button b_normal' onclick=\"javascript:evt_btnWkfUrge_onclick('" + view.getdb_alias() + "','" + view.getdb_debug() + "','" + view.getsource_charset() + "','" + view.gettarget_charset() + "','%sn%');\" value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "WKF_URGE");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='10%' align='center'");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        return laltColRelation;
    }

    /**
     * getDataViewPageAction
     *
     * @param view View
     * @return String
     */
    private String getDataViewPageAction(View view) {
        StringBuffer stbPageActionURI = new StringBuffer(view.getRequest_URI());

        stbPageActionURI.append("?prgAction=dataview_change_page");
        stbPageActionURI.append("&db_alias=");
        stbPageActionURI.append(view.getdb_alias()==null ? "" : view.getdb_alias());
        stbPageActionURI.append("&db_debug=");
        stbPageActionURI.append(view.getdb_debug() ? "true" : "false");
        stbPageActionURI.append("&source_charset=");
        stbPageActionURI.append(view.getsource_charset()==null ? "" : view.getsource_charset());
        stbPageActionURI.append("&target_charset=");
        stbPageActionURI.append(view.gettarget_charset()==null ? "" : view.gettarget_charset());
        stbPageActionURI.append("&flow_code=");
        stbPageActionURI.append(view.getFlow_code()==null ? "" : view.getFlow_code());
        stbPageActionURI.append("&flow_main_table=");
        stbPageActionURI.append(view.getFlow_main_table()==null ? "" : view.getFlow_main_table());
        stbPageActionURI.append("&find_flow_num=");
        stbPageActionURI.append((view.getFind_flow_num()==null ? "" : view.getFind_flow_num()));
        stbPageActionURI.append("&find_empno=");
        stbPageActionURI.append((view.getFind_empno()==null ? "" : view.getFind_empno()));
        stbPageActionURI.append("&find_empno_app=");
        stbPageActionURI.append((view.getFind_empno_app()==null ? "" : view.getFind_empno_app()));
        stbPageActionURI.append("&find_status=");
        stbPageActionURI.append((view.getFind_status()==null ? "" : view.getFind_status()));
        stbPageActionURI.append("&find_app_date_to=");
        stbPageActionURI.append((view.getFind_app_date_to()==null ? "" : view.getFind_app_date_to()));
        stbPageActionURI.append("&find_app_date_from=");
        stbPageActionURI.append((view.getFind_app_date_from()==null ? "" : view.getFind_app_date_from()));

        return stbPageActionURI.toString();
    }

    /**
     * getDataViewWhere
     *
     * @param view View
     * @return Object
     */
    private String getDataViewWhere(View view) throws Exception{
        StringBuffer stbCondition=new StringBuffer();

        stbCondition.append("empno=pnl_employee.employee_no ");
        stbCondition.append("and status=floweng_setup.flow_no ");
        stbCondition.append("and floweng_setup.flow_code=");
        stbCondition.append(view.getFlow_main_table());
        stbCondition.append(".flow_code ");

        //前端有輸入flow_num為條件值
        if(view.getFind_flow_num()!=null && !view.getFind_flow_num().equals("")){
            stbCondition.append("and flow_num='");
            stbCondition.append(view.getFind_flow_num());
            stbCondition.append("' ");
        }

        //前端有輸入當事人工號為條件值
        if(view.getFind_empno()!=null && !view.getFind_empno().equals("")){
            stbCondition.append("and empno='");
            stbCondition.append(view.getFind_empno());
            stbCondition.append("' ");
        }

        //前端有輸入申請人工號為條件值
        if(view.getFind_empno_app()!=null && !view.getFind_empno_app().equals("")){
            stbCondition.append("and empno_app='");
            stbCondition.append(view.getFind_empno_app());
            stbCondition.append("' ");
        }

        //前端有輸入申請日為條件值

        if(view.getFind_app_date_from()!=null && !view.getFind_app_date_from().equals("")){
          stbCondition.append("and ");
          stbCondition.append(DBFunction.getDateToStrSyntax(DBFunction.getDBType(view.getdb_alias()), "app_date", "yyyy/MM/dd"));
          stbCondition.append(">='");
          stbCondition.append(view.getFind_app_date_from());
          stbCondition.append("' ");
        }
      if(view.getFind_app_date_to()!=null && !view.getFind_app_date_to().equals("")){
            stbCondition.append("and ");
            stbCondition.append(DBFunction.getDateToStrSyntax(DBFunction.getDBType(view.getdb_alias()),"app_date","yyyy/MM/dd"));
            stbCondition.append("<='");
            stbCondition.append(view.getFind_app_date_to());
            stbCondition.append("' ");
        }


        //前端有輸入站別為條件值
        if(view.getFind_status()!=null && !view.getFind_status().equals("")){
            stbCondition.append("and status='");
            stbCondition.append(view.getFind_status());
            stbCondition.append("'");
        }

        return stbCondition.toString();
    }

    /**
     * initAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    void initAction(View view, String pstrUserNO) {
        view.setFind_status_list(WebFunction.transferRSToHtmlOption(gqbnDAO,"select flow_no,flow_desc from floweng_setup where flow_code='" + view.getFlow_code() + "'",""));
    }

    /**
     * dataViewAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    String dataViewAction(View view, String pstrUserNO) throws Exception{
        if(view.getPrgAction()!=null && view.getPrgAction().equals("dataview_change_page")){
            view.setDataViewHtml(getDataViewHtml(view.getDataView(),view.getDataViewForm(),view));
        }
        return "";
    }
}
